package com.sundhage.util {
	import flash.display.*;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	public class JSImageLoader extends Sprite {
		private var waiter:Sprite;
		private var loader:Loader;
		private var contentWidth:int;
		private var contentHeight:int;
		public var data:Object;
		private function loadComplete(e:Event):void {
			removeChild(waiter);
			addChild(loader);
			dispatchEvent(e);
		}
		
		private function loading(e:Event):void {
			dispatchEvent(e);
		}
		
		public function load(url:String, context:LoaderContext=null, data:Object = null):void {
			//trace(url);
			this.data = data;
			loader = new Loader();
		
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loading);
			//trace("URL: "+url);
			loader.load(new URLRequest(url), context);
		}


		private function createWaiter():Sprite {
			waiter = new Sprite();
			waiter.graphics.beginFill(0x0, 1);
			waiter.graphics.drawRect(0,0,contentWidth,contentHeight);
			waiter.graphics.endFill();
			return waiter;
		}
		
		public function JSImageLoader(waiter:DisplayObject = null, contentWidth:int = 15, contentHeight:int = 15) {
			this.contentWidth = contentWidth;
			this.contentHeight = contentHeight;
			if (waiter) {				
				this.waiter = Sprite(waiter);
				waiter.x = contentWidth/2+waiter.width/2;
				waiter.y = contentHeight/2+waiter.height/2;
			}
			else
				this.waiter = createWaiter();

			addChild(this.waiter);
		}
		
	}
}